basicmicrofandomcom-20200214-history
MBasic: Assignment and Data Commands
Assignments and Data Commands Intro This chapter includes the "normal" BASIC commands that are included with most versions of BASIC, as well as commands specific to Atom BASIC. Read it carefully: some familiar commands may be defined somewhat differently in Atom BASIC. This chapter contains the following sections: *Assignment and Data Commands **Let **Clear **Lookdown **Lookup **Swap **Push **Pop Conventions Used in this Chapter = (LET) Assigns the value of an expression or a variable to the target variable. Syntax :{let} target variable = expression ::expression is any valid numeric expression, variable, or constant. The word LET is optional and may be included to improve readability. Note:' The target variable should have a number type sufficient to store the largest expected result of the expression. If the target variable is too small only the lowest significant bits will be assigned. See the examples below. '''''Examples If x is a byte variable, and y has the numeric value 13, :x=y*5 will assign the value 65 to variable x. Note, however, that using the same values, :x=y*20 will assign the value 4 to variable x. This is because the actual result, 260, is too large to be stored in one byte so only the 8 lowest significant bits are stored. CLEAR Sets all user RAM to zeros. This can be used to clear RAM after a reset (so that its state will be known) or within a program to clear all variables to zeros. Note:' Atom BASIC uses CLEAR differently from other basics. '''''Syntax :clear LOOKDOWN Lookdown checks through items in a list of variables looking for the first one that matches the specified criterion. The index number (beginning with 0) of the matching list item is passed to the target variable. The scan goes from left to right and stops as soon as the operator condition is met. Note:' If the condition is not met, the target variable will be unchanged. '''''Syntax :lookdown value,{operator,} [list],target ::value is the variable or constant to be compared operator is a comparison operator (see page 62) - default is "=" ::list is a list of constants or variables, up to 16 bits each target is a variable to store the resulting index value Examples :x var byte :y var byte :x = 120 :lookdown x,>,3,10,18,36,50,66,100,130,150,200,240,y will set y = 6 Instead of numeric constants, defined constants, variables or array elements may be used in the list. However, in the case of: :x var byte :y var byte :x = 120 :lookdown x,=,3,10,18,36,50,66,100,130,150,200,240,y the condition is not met so the value of "y" will be unchanged. LOOKUP Uses an index number to select a value from a list. Each item in the list corresponds to a position of 0, 1, 2, 3... etc. in the list. Note:' If the index number exceeds the number of items in the list, the target variable will be unchanged. '''''Syntax :lookup index,[list],target ::index is the position (constant or variable) to be retrieved from the list ::list is a list of constants or variables, up to 16 bits each ::target is a variable to store the resulting list item Examples :temp var byte :value var byte :temp = 2 :lookup temp,10,20,30,40,50,60,70,80,90,value will set "value" = 30 (remember that counting starts from zero). Summary of LOOKUP and LOOKDOWN LOOKUP is, in effect, the converse of LOOKDOWN. Lookup takes an index and returns the corresponding value from a table, lookdown compares a value to elements in a table, and returns the corresponding index. SWAP Exchanges the values of two variables. Note:' The variables should be of the same size to prevent possible errors. '''''Syntax :swap variable1,variable2 Examples :ant var word :bat var word :ant = 3800 :bat = 27 :swap ant,bat Now ant = 27 and bat = 3800. Swap eliminates the need for an intermediate variable and shortens program length when compared with the alternative method shown below: :ant var word :bat var word :cat var word :ant = 3800 :bat = 27 :let cat = ant ; now cat = 3800 :let ant = bat ; now ant = 27 :let bat = cat ; now bat = 3800 PUSH, POP PUSH Stores a 32 bit value on the stack. POP Retrieves a 32 bit value from the stack. Important:' PUSH must always be matched by a subsequent POP instruction before any other stack-oriented commands (e.g. GOSUB, RETURN, EXCEPTION) are used. '''''Syntax :push variable :pop variable ::variable may be of any type. For push the value will be padded with high order zeros to fill 32 bits if necessary. For pop the high order bits will be truncated if necessary to fit the variable. Variable types should be matched for predictability. While it's possible, for example, to PUSH a long variable, and subsequently POP a word or byte variable, it's less confusing to stick to matched types. If you POP a word or byte variable only the low order bits will be stored. ''Warning: PUSH and POP are no longer basic commands with the current versions of Basic Micro Studio. The compiler will assume that you are most likely trying to do inline assembly language and will probably generate an error (invalid opcode) in the assembly stage. For those users who still think they need this, there maybe a work around that may work, but I would personally look for better solutions. The work uses additional inline assembly. :myvar var long :... :mov.l @MYVAR, er1 ; was push myvar :push.l er1 : :... :pop.l er1 :mov.l er1, @MYVAR ; was pop myvar Note: in the inline assembly you must enter the variable name in all capital letters.''